/**
 *
 */
package de.cbf.cam.dsa.facet.weapon_cc.editor;

import static de.cbf.util.ui.test_support.ConstantsSWTBOTUtil.DEFAULT_SWT_BOT_KEY;

import java.util.Arrays;
import java.util.UUID;

import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

import de.cbf.cam.dsa.facet.weapon_cc.test_support.ConstantsSWTBOTWeaponCC;
import de.cbf.util.entity.facet.edit.composites.AbstractCompositeEditFacet;
import de.cbf.util.entity.facet.edit.util.FieldDecoratorUtil;
import de.cbf.util.ui.listener.UiChangeEvent;
import de.cbf.util.ui.swt.util.SWT_Util;

/**
 * @author Nebelritter
 *
 */
public class CompositeEditWeaponCC extends AbstractCompositeEditFacet {

	public static final int CMD_TALENTS_SELECTED 	=  1;
	public static final int CMD_TALENTS_ADD			=  2;
	public static final int CMD_TALENTS_REMOVE		=  3;

	public static final int IDX_TP					=  4;
	public static final int IDX_TP_KK_BASE 			=  5;
	public static final int IDX_TP_KK_STEP 			=  6;
	public static final int IDX_LENGTH				=  7;
	public static final int IDX_BF					=  8;
	public static final int IDX_INI					=  9;
	public static final int IDX_WM_AT				= 10;
	public static final int IDX_WM_PA				= 11;
	public static final int IDX_DK_H 				= 12;
	public static final int IDX_DK_N 				= 13;
	public static final int IDX_DK_S 				= 14;
	public static final int IDX_DK_P 				= 15;
	public static final int IDX_SPECIAL_TWO_HANDED	= 16;
	public static final int IDX_SPECIAL_IMPROVISED	= 17;
	public static final int IDX_SPECIAL_PRIVILEGED	= 18;


	protected static final String GROUP_TALENTS_TEXT		= "Talents..";
	protected static final String TALENTS_TEXT				= "Talents for Weapon to be used with:";
	protected static final String TALENTS_ADD_TEXT			= "Add..";
	protected static final String TALENTS_REMOVE_TEXT		= "Remove";
	protected static final String GROUP_WEAPON_VALUES_TEXT	= "Weapon values";
	protected static final String GROUP_WEAPON_VALUES_TEXT_DOTS	= "...";
	protected static final String GROUP_WEAPON_VALUES_TEXT_FOR = "for";
	protected static final String TP_TEXT 					= "Tp:";
	protected static final String TP_KK_BASE_TEXT			= "Tp/KK Base:";
	protected static final String TP_KK_STEP_TEXT			= "Tp/KK Step:";
	protected static final String LENGTH_TEXT				= "Length:";
	protected static final String BF_TEXT					= "Bf:";
	protected static final String INI_TEXT					= "Ini:";
	protected static final String WM_AT_TEXT				= "Wm AT:";
	protected static final String WM_PA_TEXT				= "Wm PA:";
	protected static final String GROUP_DK_TEXT				= "Distance classes..";
	protected static final String DK_H_TEXT					= "H:";
	protected static final String DK_N_TEXT					= "N:";
	protected static final String DK_S_TEXT					= "S:";
	protected static final String DK_P_TEXT					= "P:";
	protected static final String GROUP_SPECIALS_TEXT		= "Specials..";
	protected static final String SPEC_TWO_HANDED_TEXT		= "Two-handed:";
	protected static final String SPEC_PRIVILEGED_TEXT		= "privileged:";
	protected static final String SPEC_IMPROVISED_TEXT		= "improvised:";

	protected static final String ERROR_ENTRY_TP_TEXT = "Please enter tp in format xWy+z";
	protected static final String GROUP_WEAPON_VALUES_DISABLED_HINT = "Please choose/create talent first!";


	protected List li_Talents;
	protected Button bt_Talents_Add;
	protected Button bt_Talents_Remove;
	protected Text tx_Length;
	protected Text tx_Bf;
	protected Group grp_WeaponValues;
	protected Text tx_Tp_CC;
	protected Text tx_Tp_KK_Base;
	protected Text tx_Tp_KK_Step;

	protected Text tx_Ini;
	protected Text tx_WmAT;
	protected Text tx_WmPA;
	protected Button bt_Dk_H;
	protected Button bt_Dk_N;
	protected Button bt_Dk_S;
	protected Button bt_Dk_P;
	protected Button bt_Spec_TwoHanded;
	protected Button bt_Spec_Privileged;
	protected Button bt_Spec_Improvised;

	protected ControlDecoration decoration_Tp_CC;
	protected ControlDecoration decoration_TpKKBase;
	protected ControlDecoration decoration_TpKKStep;
	protected ControlDecoration decoration_Length;
	protected ControlDecoration decoration_Bf;
	protected ControlDecoration decoration_Ini;
	protected ControlDecoration decoration_WmAT;
	protected ControlDecoration decoration_WmPA;

	
	public CompositeEditWeaponCC(Composite parent, int style,FormToolkit toolkit) {
		super(parent, style,toolkit);		
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		if(getSelectedTalentName() != null){
			setStatusButtonTalentDeleteEnabled(true);
			setStatusWeaponValuesEnabled(true);
		}else{
			setStatusButtonTalentDeleteEnabled(false);
			setStatusWeaponValuesEnabled(false);
		}
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.entity.facet.edit.composites.AbstractCompositeEditFacet#createLayout()
	 */
	@Override
	protected GridLayout createLayout() {
		GridLayout result = new GridLayout(4,false);
		return result;
	}
	
	@Override
	protected void createContent(final Composite compositeThis){
		//bf
		Label lb_Bf = new Label(compositeThis, SWT.NONE);
		lb_Bf.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lb_Bf.setText(BF_TEXT);

		tx_Bf = new Text(compositeThis, SWT.BORDER);
		tx_Bf.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		tx_Bf.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_TEXT_BF_ID);
		tx_Bf.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(tx_Bf.getText());
				event.setIndex(IDX_BF);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for bf
		decoration_Bf = new ControlDecoration(tx_Bf,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_Bf = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_Bf.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_INTEGER);
		decoration_Bf.setImage(image_Bf);
		decoration_Bf.hide();

		//length
		Label lb_Length = new Label(compositeThis, SWT.NONE);
		lb_Length.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lb_Length.setText(LENGTH_TEXT);

		tx_Length = new Text(compositeThis, SWT.BORDER);
		tx_Length.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		tx_Length.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_TEXT_LENGTH_ID);
		tx_Length.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(tx_Length.getText());
				event.setIndex(IDX_LENGTH);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for length
		decoration_Length = new ControlDecoration(tx_Length,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_Length = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_Length.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_ANY);
		decoration_Length.setImage(image_Length);
		decoration_Length.hide();

		Composite cp_WeaponValuesTalents = createCompositeWeaponValuesTalents(compositeThis);
		cp_WeaponValuesTalents.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,4,1));

		Composite cp_SF = createCompositeSF(compositeThis);
		cp_SF.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,4,1));


	}



	/**
	 * @param compositeThis
	 * @return
	 */
	protected Composite createCompositeWeaponValuesTalents(Composite compositeThis) {
		Composite cp_WeaponValuesTalents = new Composite(compositeThis, SWT.DOUBLE_BUFFERED);
		GridLayout lo_WeaponValuesTalents = new GridLayout(4, false);
		lo_WeaponValuesTalents.marginHeight = 0;
		lo_WeaponValuesTalents.marginWidth 	= 0;
		cp_WeaponValuesTalents.setLayout(lo_WeaponValuesTalents);

		Composite cp_Talents = createCompositeTalents(cp_WeaponValuesTalents);
		cp_Talents.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,4,1));

		Composite cp_WeaponValues = createCompositeWeaponValues(cp_WeaponValuesTalents);
		cp_WeaponValues.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,4,1));

		return cp_WeaponValuesTalents;
	}

	/**
	 * @param compositeThis
	 * @return
	 */
	protected Composite createCompositeTalents(final Composite compositeThis) {
		Group grp_Talents = new Group(compositeThis, SWT.DOUBLE_BUFFERED);
		grp_Talents.setLayout(new GridLayout(2, false));
		grp_Talents.setText(GROUP_TALENTS_TEXT);

		// special two handed
		Label lb_Talents = new Label(grp_Talents, SWT.NONE);
		lb_Talents.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,2,1));
		lb_Talents.setText(TALENTS_TEXT);

		li_Talents = new List(grp_Talents, SWT.BORDER|SWT.SINGLE);
		GridData gd_Talents = new GridData(SWT.FILL, SWT.FILL, true, false,1,2);
		gd_Talents.heightHint = 30;
		li_Talents.setLayoutData(gd_Talents);
		li_Talents.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_LIST_TALENTS_ID);
		li_Talents.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				fireUICommand(CMD_TALENTS_SELECTED, (Object[])li_Talents.getSelection());
			}
		});

		bt_Talents_Add = new Button(grp_Talents, SWT.PUSH);
		bt_Talents_Add.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false,1,1));
		bt_Talents_Add.setText(TALENTS_ADD_TEXT);
		bt_Talents_Add.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_TALENT_ADD_ID);
		bt_Talents_Add.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				fireUICommand(CMD_TALENTS_ADD,(Object[])null);
			}
		});

		bt_Talents_Remove = new Button(grp_Talents, SWT.PUSH);
		bt_Talents_Remove.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false,1,1));
		bt_Talents_Remove.setText(TALENTS_REMOVE_TEXT);
		bt_Talents_Remove.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_TALENT_REMOVE_ID);
		bt_Talents_Remove.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				fireUICommand(CMD_TALENTS_REMOVE,(Object[])null);
			}
		});

		return grp_Talents;
	}

	/**
	 * @param compositeThis
	 * @return
	 */
	protected Composite createCompositeSF(final Composite compositeThis) {
		Group grp_Talents = new Group(compositeThis, SWT.DOUBLE_BUFFERED);
		grp_Talents.setLayout(new GridLayout(2, false));
		grp_Talents.setText(GROUP_TALENTS_TEXT);

		// special two handed
		Label lb_Talents = new Label(grp_Talents, SWT.NONE);
		lb_Talents.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,2,1));
		lb_Talents.setText(TALENTS_TEXT);

		li_Talents = new List(grp_Talents, SWT.BORDER|SWT.SINGLE);
		GridData gd_Talents = new GridData(SWT.FILL, SWT.FILL, true, false,1,2);
		gd_Talents.heightHint = 30;
		li_Talents.setLayoutData(gd_Talents);
		li_Talents.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_LIST_TALENTS_ID);
		li_Talents.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				fireUICommand(CMD_TALENTS_SELECTED, (Object[])li_Talents.getSelection());
			}
		});

		bt_Talents_Add = new Button(grp_Talents, SWT.PUSH);
		bt_Talents_Add.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false,1,1));
		bt_Talents_Add.setText(TALENTS_ADD_TEXT);
		bt_Talents_Add.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_TALENT_ADD_ID);
		bt_Talents_Add.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				fireUICommand(CMD_TALENTS_ADD,(Object[])null);
			}
		});

		bt_Talents_Remove = new Button(grp_Talents, SWT.PUSH);
		bt_Talents_Remove.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false,1,1));
		bt_Talents_Remove.setText(TALENTS_REMOVE_TEXT);
		bt_Talents_Remove.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_TALENT_REMOVE_ID);
		bt_Talents_Remove.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				fireUICommand(CMD_TALENTS_REMOVE,(Object[])null);
			}
		});

		return grp_Talents;
	}



	/**
	 * @param cp_WeaponValuesTalents
	 * @return
	 */
	protected Composite createCompositeWeaponValues(Composite cp_WeaponValuesTalents) {
		grp_WeaponValues = new Group(cp_WeaponValuesTalents, SWT.DOUBLE_BUFFERED);
		grp_WeaponValues.setLayout(new GridLayout(4, false));
		grp_WeaponValues.setText(GROUP_WEAPON_VALUES_TEXT);

		//tp
		Label lb_TP = new Label(grp_WeaponValues, SWT.NONE);
		lb_TP.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lb_TP.setText(TP_TEXT);

		tx_Tp_CC = new Text(grp_WeaponValues, SWT.BORDER);
		tx_Tp_CC.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		tx_Tp_CC.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_TEXT_TP_ID);
		tx_Tp_CC.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditWeaponCC.this);
				event.setNewValue(tx_Tp_CC.getText());
				event.setIndex(IDX_TP);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for weight
		decoration_Tp_CC = new ControlDecoration(tx_Tp_CC,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_Tp = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_Tp_CC.setDescriptionText(ERROR_ENTRY_TP_TEXT);
		decoration_Tp_CC.setImage(image_Tp);
		decoration_Tp_CC.hide();

		//ini
		Label lb_Ini = new Label(grp_WeaponValues, SWT.NONE);
		lb_Ini.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lb_Ini.setText(INI_TEXT);

		tx_Ini = new Text(grp_WeaponValues, SWT.BORDER);
		tx_Ini.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		tx_Ini.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_TEXT_INI_ID);
		tx_Ini.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditWeaponCC.this);
				event.setNewValue(tx_Ini.getText());
				event.setIndex(IDX_INI);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for ini
		decoration_Ini = new ControlDecoration(tx_Ini,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_Ini = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_Ini.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_INTEGER);
		decoration_Ini.setImage(image_Ini);
		decoration_Ini.hide();


		//tp kk base
		Label lb_TP_KK_Base = new Label(grp_WeaponValues, SWT.NONE);
		lb_TP_KK_Base.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lb_TP_KK_Base.setText(TP_KK_BASE_TEXT);

		tx_Tp_KK_Base = new Text(grp_WeaponValues, SWT.BORDER);
		tx_Tp_KK_Base.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		tx_Tp_KK_Base.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_TEXT_TP_KK_BASE_ID);
		tx_Tp_KK_Base.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditWeaponCC.this);
				event.setNewValue(tx_Tp_KK_Base.getText());
				event.setIndex(IDX_TP_KK_BASE);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for weight
		decoration_TpKKBase = new ControlDecoration(tx_Tp_KK_Base,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_TpKKBase = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_TpKKBase.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_INTEGER);
		decoration_TpKKBase.setImage(image_TpKKBase);
		decoration_TpKKBase.hide();

		//tp kk step
		Label lb_TP_KK_Step = new Label(grp_WeaponValues, SWT.NONE);
		lb_TP_KK_Step.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lb_TP_KK_Step.setText(TP_KK_STEP_TEXT);

		tx_Tp_KK_Step = new Text(grp_WeaponValues, SWT.BORDER);
		tx_Tp_KK_Step.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		tx_Tp_KK_Step.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_TEXT_TP_KK_STEP_ID);
		tx_Tp_KK_Step.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditWeaponCC.this);
				event.setNewValue(tx_Tp_KK_Step.getText());
				event.setIndex(IDX_TP_KK_STEP);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for weight
		decoration_TpKKStep = new ControlDecoration(tx_Tp_KK_Step,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_TpKKStep = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_TpKKStep.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_INTEGER);
		decoration_TpKKStep.setImage(image_TpKKStep);
		decoration_TpKKStep.hide();

		// WM AT
		Label lb_WmAT = new Label(grp_WeaponValues, SWT.NONE);
		lb_WmAT.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lb_WmAT.setText(WM_AT_TEXT);

		tx_WmAT = new Text(grp_WeaponValues, SWT.BORDER);
		tx_WmAT.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		tx_WmAT.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_TEXT_WM_AT_ID);
		tx_WmAT.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditWeaponCC.this);
				event.setNewValue(tx_WmAT.getText());
				event.setIndex(IDX_WM_AT);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for ini
		decoration_WmAT = new ControlDecoration(tx_WmAT,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_WmAT = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_WmAT.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_INTEGER);
		decoration_WmAT.setImage(image_WmAT);
		decoration_WmAT.hide();

		// WM PA
		Label lb_WmPA = new Label(grp_WeaponValues, SWT.NONE);
		lb_WmPA.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lb_WmPA.setText(WM_PA_TEXT);

		tx_WmPA = new Text(grp_WeaponValues, SWT.BORDER);
		tx_WmPA.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		tx_WmPA.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_TEXT_WM_PA_ID);
		tx_WmPA.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditWeaponCC.this);
				event.setNewValue(tx_WmPA.getText());
				event.setIndex(IDX_WM_PA);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for ini
		decoration_WmPA = new ControlDecoration(tx_WmPA,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_WmPA = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_WmPA.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_INTEGER);
		decoration_WmPA.setImage(image_WmPA);
		decoration_WmPA.hide();

		Composite cp_SpecialsDk = new Composite(grp_WeaponValues, SWT.DOUBLE_BUFFERED);
		cp_SpecialsDk.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,4,1));
		GridLayout lo_SpecialsDk = new GridLayout(2, false);
		lo_SpecialsDk.marginHeight = 0;
		lo_SpecialsDk.marginWidth  = 0;
		cp_SpecialsDk.setLayout(lo_SpecialsDk);

		Composite cp_Specials = createCompositeSpecials(cp_SpecialsDk);
		cp_Specials.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,1,1));

		Composite cp_DK = createCompositeDK(cp_SpecialsDk);
		cp_DK.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,1,1));

		return grp_WeaponValues;
	}


	/**
	 * @param compositeThis
	 */
	protected Composite createCompositeSpecials(final Composite compositeThis) {
		Group grp_Specials = new Group(compositeThis, SWT.DOUBLE_BUFFERED);
		grp_Specials.setLayout(new GridLayout(6, false));
		grp_Specials.setText(GROUP_SPECIALS_TEXT);

		// special two handed
		Label lb_Special_TwoHanded = new Label(grp_Specials, SWT.NONE);
		lb_Special_TwoHanded.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		lb_Special_TwoHanded.setText(SPEC_TWO_HANDED_TEXT);

		bt_Spec_TwoHanded = new Button(grp_Specials, SWT.CHECK);
		bt_Spec_TwoHanded.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		bt_Spec_TwoHanded.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_SPEC_TWO_HANDED_ID);
		bt_Spec_TwoHanded.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(bt_Spec_TwoHanded.getSelection());
				event.setIndex(IDX_SPECIAL_TWO_HANDED);
				fireUIChangeEvent(event);
			}
		});

		// special privileged
		Label lb_Special_Privileged = new Label(grp_Specials, SWT.NONE);
		lb_Special_Privileged.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		lb_Special_Privileged.setText(SPEC_PRIVILEGED_TEXT);

		bt_Spec_Privileged = new Button(grp_Specials, SWT.CHECK);
		bt_Spec_Privileged.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		bt_Spec_Privileged.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_SPEC_PRIVILEGED_ID);
		bt_Spec_Privileged.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(bt_Spec_Privileged.getSelection());
				event.setIndex(IDX_SPECIAL_PRIVILEGED);
				fireUIChangeEvent(event);
			}
		});

		// special improvised
		Label lb_Special_Improvised = new Label(grp_Specials, SWT.NONE);
		lb_Special_Improvised.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		lb_Special_Improvised.setText(SPEC_IMPROVISED_TEXT);

		bt_Spec_Improvised = new Button(grp_Specials, SWT.CHECK);
		bt_Spec_Improvised.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		bt_Spec_Improvised.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_SPEC_IMPROVISED_ID);
		bt_Spec_Improvised.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(bt_Spec_Improvised.getSelection());
				event.setIndex(IDX_SPECIAL_IMPROVISED);
				fireUIChangeEvent(event);
			}
		});

		return grp_Specials;
	}

	/**
	 * @param compositeThis
	 */
	protected Composite createCompositeDK(final Composite compositeThis) {
		Group grp_DK = new Group(compositeThis, SWT.DOUBLE_BUFFERED);
		grp_DK.setLayout(new GridLayout(8, false));
		grp_DK.setText(GROUP_DK_TEXT);

		// DK H
		Label lb_Dk_H = new Label(grp_DK, SWT.NONE);
		lb_Dk_H.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		lb_Dk_H.setText(DK_H_TEXT);

		bt_Dk_H = new Button(grp_DK, SWT.CHECK);
		bt_Dk_H.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		bt_Dk_H.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_DK_H_ID);
		bt_Dk_H.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(bt_Dk_H.getSelection());
				event.setIndex(IDX_DK_H);
				fireUIChangeEvent(event);
			}
		});

		// DK N
		Label lb_Dk_N = new Label(grp_DK, SWT.NONE);
		lb_Dk_N.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		lb_Dk_N.setText(DK_N_TEXT);

		bt_Dk_N = new Button(grp_DK, SWT.CHECK);
		bt_Dk_N.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		bt_Dk_N.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_DK_N_ID);
		bt_Dk_N.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(bt_Dk_N.getSelection());
				event.setIndex(IDX_DK_N);
				fireUIChangeEvent(event);
			}
		});

		// DK S
		Label lb_Dk_S = new Label(grp_DK, SWT.NONE);
		lb_Dk_S.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		lb_Dk_S.setText(DK_S_TEXT);

		bt_Dk_S = new Button(grp_DK, SWT.CHECK);
		bt_Dk_S.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		bt_Dk_S.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_DK_S_ID);
		bt_Dk_S.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(bt_Dk_S.getSelection());
				event.setIndex(IDX_DK_S);
				fireUIChangeEvent(event);
			}
		});

		// DK P
		Label lb_Dk_P = new Label(grp_DK, SWT.NONE);
		lb_Dk_P.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		lb_Dk_P.setText(DK_P_TEXT);

		bt_Dk_P = new Button(grp_DK, SWT.CHECK|SWT.LEFT);
		bt_Dk_P.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,1,1));
		bt_Dk_P.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTWeaponCC.FACET_WEAPON_CC_BT_DK_P_ID);
		bt_Dk_P.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(bt_Dk_P.getSelection());
				event.setIndex(IDX_DK_P);
				fireUIChangeEvent(event);
			}
		});

		return grp_DK;
	}

	@Override
	protected void adjustInitialUIState() {
		setStatusButtonTalentDeleteEnabled(false);
		setStatusWeaponValuesEnabled(false);
	}

	public void setStatusButtonTalentDeleteEnabled(boolean enabled){
		if(this.isEnabled()){
			bt_Talents_Remove.setEnabled(enabled);
		}
	}

	/**
	 * @param enabled
	 */
	public void setStatusWeaponValuesEnabled(boolean enabled) {
		grp_WeaponValues.setEnabled(enabled);
		SWT_Util.setChildrenEditable(grp_WeaponValues, enabled);
		if( ! enabled){
			grp_WeaponValues.setToolTipText(GROUP_WEAPON_VALUES_DISABLED_HINT);
		}else{
			grp_WeaponValues.setToolTipText(null);
		}
	}

	public void setGroupWeaponValuesTalentText(String talentName){
		if(talentName != null && talentName.length() > 0){
			grp_WeaponValues.setText(GROUP_WEAPON_VALUES_TEXT+" "+GROUP_WEAPON_VALUES_TEXT_FOR+" "+talentName);
		}else{
			grp_WeaponValues.setText(GROUP_WEAPON_VALUES_TEXT+GROUP_WEAPON_VALUES_TEXT_DOTS);
		}

	}
	
	public String getSelectedTalentName(){
		String [] selection = li_Talents.getSelection();
		if( selection != null && selection.length > 0){
			return li_Talents.getSelection()[0];
		}
		return null;
	}
	
	public UUID getSelectedTalentUUID(){
		String [] selection = li_Talents.getSelection();
		if( selection != null && selection.length > 0){
			Object selectedData = li_Talents.getData(li_Talents.getSelection()[0]);
			return (UUID)selectedData;
		}
		return null;
	}

	public void setSelectedTalent(String talent){
		//first check if talent that is to be set is already selected
		if(talent != null){
			if(li_Talents.getSelectionCount() > 0 && ! li_Talents.getSelection()[0].equals(talent)){
				//new talent set, select and update text
				for (int i = 0; i < li_Talents.getItems().length; i++) {
					if(li_Talents.getItems()[i].equals(talent)){
						li_Talents.select(i); //will fire events
						return;
					}
				}
			}			
		}
		//talent is not found or null -> erase values of weapon
		setGroupWeaponValuesTalentText(null);
		setStatusWeaponValuesEnabled(false);		
	}

	public String[] getTalents(){
		return li_Talents.getItems();
	}
	
	public void setTalents(String[] talentNames,UUID[] talentUuids){
		if(talentNames != null && ! Arrays.deepEquals(li_Talents.getItems(), talentNames)){			
			String selected = getSelectedTalentName();
			li_Talents.setItems(talentNames);
			for (int i = 0; i < talentNames.length; i++) {
				li_Talents.setData(talentNames[i],talentUuids[i]);
			}
			setSelectedTalent(selected);			
		}
	}

	public String getTp(){
		return tx_Tp_CC.getText();
	}
	public void setTp(String newTp){
		if(newTp == null){
			tx_Tp_CC.setText("");
		}
		if( ! tx_Tp_CC.getText().equals(newTp)){
			tx_Tp_CC.setText(newTp);
		}
	}

	public String getTpKKBase(){
		return tx_Tp_KK_Base.getText();
	}
	public void setTpKKBase(String newTpKKbase){
		if(newTpKKbase == null){
			tx_Tp_KK_Base.setText("");
		}
		if( ! tx_Tp_KK_Base.getText().equals(newTpKKbase)){
			tx_Tp_KK_Base.setText(newTpKKbase);
		}
	}

	public String getTpKKStep(){
		return tx_Tp_KK_Step.getText();
	}
	public void setTpKKStep(String newTpKKstep){
		if(newTpKKstep == null){
			tx_Tp_KK_Step.setText("");
		}
		if( ! tx_Tp_KK_Step.getText().equals(newTpKKstep)){
			tx_Tp_KK_Step.setText(newTpKKstep);
		}
	}

	public String getLength(){
		return tx_Length.getText();
	}
	public void setLength(String newLength){
		if(newLength == null){
			tx_Length.setText("");
		}
		if( ! tx_Length.getText().equals(newLength)){
			tx_Length.setText(newLength);
		}
	}

	public String getBf(){
		return tx_Bf.getText();
	}
	public void setBf(String newBf){
		if(newBf == null){
			tx_Bf.setText("");
		}
		if( ! tx_Bf.getText().equals(newBf)){
			tx_Bf.setText(newBf);
		}
	}

	public String getIni(){
		return tx_Ini.getText();
	}
	public void setIni(String newIni){
		if(newIni == null){
			tx_Ini.setText("");
		}
		if( ! tx_Ini.getText().equals(newIni)){
			tx_Ini.setText(newIni);
		}
	}

	public String getWmAT(){
		return tx_WmAT.getText();
	}
	public void setWmAT(String newWmAt){
		if(newWmAt == null){
			tx_WmAT.setText("");
		}
		if( ! tx_WmAT.getText().equals(newWmAt)){
			tx_WmAT.setText(newWmAt);
		}
	}

	public String getWmPA(){
		return tx_WmPA.getText();
	}
	public void setWmPA(String newWmPa){
		if(newWmPa == null){
			tx_WmPA.setText("");
		}
		if( ! tx_WmPA.getText().equals(newWmPa)){
			tx_WmPA.setText(newWmPa);
		}
	}

	public Boolean getBtDk_H_Selected(){
		return bt_Dk_H.getSelection();
	}
	public void setBtDk_H_Selected(Boolean newSelected){
		if(newSelected == null){
			bt_Dk_H.setSelection(false);
		}
		if( ! bt_Dk_H.getSelection()==newSelected){
			bt_Dk_H.setSelection(newSelected);
		}
	}

	public Boolean getBtDk_N_Selected(){
		return bt_Dk_N.getSelection();
	}
	public void setBtDk_N_Selected(Boolean newSelected){
		if(newSelected == null){
			bt_Dk_N.setSelection(false);
		}
		if( ! bt_Dk_N.getSelection()==newSelected){
			bt_Dk_N.setSelection(newSelected);
		}
	}

	public Boolean getBtDk_S_Selected(){
		return bt_Dk_S.getSelection();
	}
	public void setBtDk_S_Selected(Boolean newSelected){
		if(newSelected == null){
			bt_Dk_S.setSelection(false);
		}
		if( ! bt_Dk_S.getSelection()==newSelected){
			bt_Dk_S.setSelection(newSelected);
		}
	}

	public Boolean getBtDk_P_Selected(){
		return bt_Dk_P.getSelection();
	}
	public void setBtDk_P_Selected(Boolean newSelected){
		if(newSelected == null){
			bt_Dk_P.setSelection(false);
		}
		if( ! bt_Dk_P.getSelection()==newSelected){
			bt_Dk_P.setSelection(newSelected);
		}
	}

	public Boolean getBtSpecTwoHandedSelected(){
		return bt_Spec_TwoHanded.getSelection();
	}
	public void setBtSpecTwoHandedSelected(Boolean newSelected){
		if(newSelected == null){
			bt_Spec_TwoHanded.setSelection(false);
		}
		if( ! bt_Spec_TwoHanded.getSelection()==newSelected){
			bt_Spec_TwoHanded.setSelection(newSelected);
		}
	}

	public Boolean getBtSpecPrivilegedSelected(){
		return bt_Spec_Privileged.getSelection();
	}
	public void setBtSpecPrivilegedSelected(Boolean newSelected){
		if(newSelected == null){
			bt_Spec_Privileged.setSelection(false);
		}
		if( ! bt_Spec_Privileged.getSelection()==newSelected){
			bt_Spec_Privileged.setSelection(newSelected);
		}
	}

	public Boolean getBtSpecImprovisedSelected(){
		return bt_Spec_Improvised.getSelection();
	}
	public void setBtSpecImprovisedSelected(Boolean newSelected){
		if(newSelected == null){
			bt_Spec_Improvised.setSelection(false);
		}
		if( ! bt_Spec_Improvised.getSelection()==newSelected){
			bt_Spec_Improvised.setSelection(newSelected);
		}
	}

	public void setDecorationTpStatus(boolean active){
		if(active){
			decoration_Tp_CC.show();
			tx_Tp_CC.setBackground(cl_Text_red);
		}else{
			tx_Tp_CC.setBackground(cl_Text_list_bg);
			decoration_Tp_CC.hide();
		}
	}

	public void setDecorationTpKKBaseStatus(boolean active){
		if(active){
			decoration_TpKKBase.show();
			tx_Tp_KK_Base.setBackground(cl_Text_red);
		}else{
			decoration_TpKKBase.hide();
			tx_Tp_KK_Base.setBackground(cl_Text_list_bg);
		}
	}

	public void setDecorationTpKKStepStatus(boolean active){
		if(active){
			decoration_TpKKStep.show();
			tx_Tp_KK_Step.setBackground(cl_Text_red);
		}else{
			decoration_TpKKStep.hide();
			tx_Tp_KK_Step.setBackground(cl_Text_list_bg);
		}
	}

	public void setDecorationLengthStatus(boolean active){
		if(active){
			decoration_Length.show();
			tx_Length.setBackground(cl_Text_red);
		}else{
			decoration_Length.hide();
			tx_Length.setBackground(cl_Text_list_bg);
		}
	}

	public void setDecorationBfStatus(boolean active){
		if(active){
			decoration_Bf.show();
			tx_Bf.setBackground(cl_Text_red);
		}else{
			decoration_Bf.hide();
			tx_Bf.setBackground(cl_Text_list_bg);
		}
	}
	public void setDecorationIniStatus(boolean active){
		if(active){
			decoration_Ini.show();
			tx_Ini.setBackground(cl_Text_red);
		}else{
			decoration_Ini.hide();
			tx_Ini.setBackground(cl_Text_list_bg);
		}
	}
	public void setDecorationWmATStatus(boolean active){
		if(active){
			decoration_WmAT.show();
			tx_WmAT.setBackground(cl_Text_red);
		}else{
			decoration_WmAT.hide();
			tx_WmAT.setBackground(cl_Text_list_bg);
		}
	}
	public void setDecorationWmPAStatus(boolean active){
		if(active){
			decoration_WmPA.show();
			tx_WmPA.setBackground(cl_Text_red);
		}else{
			decoration_WmPA.hide();
			tx_WmPA.setBackground(cl_Text_list_bg);
		}
	}

	
}
